package com.nondo.dean.saprfc.service;

import com.nondo.dean.saprfc.common.DateUtils;
import com.nondo.dean.saprfc.common.SapUtil;
import com.nondo.dean.saprfc.model.CustomerSalesReportBean;
import com.nondo.dean.saprfc.model.ProductBean;
import com.nondo.dean.saprfc.model.param.CustomerSalesReportParam;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoTable;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xiaofan
 * @Description: 客户销售报表业务处理类
 * @throws
 * @date 2018-06-2915:27
 * @Version
 */
@Component
public class CustomerSalesReportService {
    /**
     * 日期标示
     */
    //本年
    private static final Integer DATE_MARK_YEAR = 1;
    //本月
    private static final Integer DATE_MARK_MONTH = 2;
    //当天
    private static final Integer DATE_MARK_DAY = 3;

    private static final String START_DATE = "startDate";
    private static final String END_DATE = "endDate";

    /**
     * 年 月  日 判断
     * @param param
     * @return
     */
    public Map<String,String> dateCheck(CustomerSalesReportParam param){
        Map<String,String> newMap = new HashMap<>();
        if(param.getDate() !=null){
            if(CustomerSalesReportService.DATE_MARK_YEAR.equals(param.getDate())){
                newMap.put(CustomerSalesReportService.START_DATE, DateUtils.getDateText(DateUtils.getBeginDayOfYear(),DateUtils.YMD_BREAK));
                newMap.put(CustomerSalesReportService.END_DATE,DateUtils.getDateText(DateUtils.getEndDayOfYesterDay(),DateUtils.YMD_BREAK));
            }else if(CustomerSalesReportService.DATE_MARK_MONTH.equals(param.getDate())){
                newMap.put(CustomerSalesReportService.START_DATE, DateUtils.getDateText(DateUtils.getBeginDayOfMonth(),DateUtils.YMD_BREAK));
                newMap.put(CustomerSalesReportService.END_DATE,DateUtils.getDateText(DateUtils.getEndDayOfMonth(),DateUtils.YMD_BREAK));
            }else if(CustomerSalesReportService.DATE_MARK_DAY.equals(param.getDate())){
                newMap.put(CustomerSalesReportService.START_DATE, DateUtils.getDateText(DateUtils.getDayBegin(),DateUtils.YMD_BREAK));
                newMap.put(CustomerSalesReportService.END_DATE,DateUtils.getDateText(DateUtils.getDayEnd(),DateUtils.YMD_BREAK));
            }
        }else{
            newMap.put(CustomerSalesReportService.START_DATE,param.getStartDate());
            newMap.put(CustomerSalesReportService.END_DATE,param.getEndDate());
        }
        return newMap;
    }

    /**
     * 返回sap的销售排行
     * @param param
     * @param sapClient
     * @return
     */
    public List<CustomerSalesReportBean> getSapCustomerSalesList(CustomerSalesReportParam param,String sapClient){
        List<CustomerSalesReportBean> newList = new ArrayList<>();
        Map<String,String> dateMap = dateCheck(param);
        JCoFunction function = SapUtil.getFunction(sapClient,"Z_ND_CRM_002");
        function.getImportParameterList().setValue("IM_BEGDAT",dateMap.get(CustomerSalesReportService.START_DATE));
        function.getImportParameterList().setValue("IM_ENDDAT",dateMap.get(CustomerSalesReportService.END_DATE));
        function.getImportParameterList().setValue("IM_KUNNR","");
        SapUtil.executeFunction(sapClient, function);
        JCoTable table = function.getTableParameterList().getTable("EX_TABLE");
        for(int i=0; i<table.getNumRows();i++){
            CustomerSalesReportBean bean = new CustomerSalesReportBean();
            bean.setCustomerName(table.getString("NAME1"));
            bean.setCustomerCode(table.getString("KUNNR"));
            bean.setSalesTotalAmount(BigDecimal.valueOf(table.getDouble("NETWR")));
            newList.add(bean);
            table.nextRow();
        }
        return newList;
    }
    /**
     * 返回sap产品销售排行
     * @param param
     * @param sapClient
     * @return
     */
    public List<ProductBean> getSapProductSalesList(CustomerSalesReportParam param, String sapClient){
        List<ProductBean> newList = new ArrayList<>();
        Map<String,String> dateMap = dateCheck(param);
        JCoFunction function = SapUtil.getFunction(sapClient,"Z_ND_CRM_003");
        function.getImportParameterList().setValue("IM_BEGDAT",dateMap.get(CustomerSalesReportService.START_DATE));
        function.getImportParameterList().setValue("IM_ENDDAT",dateMap.get(CustomerSalesReportService.END_DATE));
        SapUtil.executeFunction(sapClient, function);
        JCoTable table = function.getTableParameterList().getTable("EX_TABLE");
        for(int i=0; i<table.getNumRows();i++){
            ProductBean productBean = new ProductBean();
            productBean.setProductName(table.getString("MAKTX"));
            productBean.setProductCode(table.getString("MATNR"));
            productBean.setNum(table.getInt("KWMENG"));
            newList.add(productBean);
            table.nextRow();
        }
        return newList;
    }
}
